using System;
using System.Windows;
using System.Windows.Controls;
using jSLCore.Client.Utility;

namespace jSLCore.Client.Presentation.BaseControl.Selection
{
    public class DataGridGoDetailsColumn : DataGridTextColumn
    {
        public DataGridGoDetailsColumn()
        {
            IsReadOnly = true;
        }

        protected override void CancelCellEdit(FrameworkElement editingElement, object uneditedValue)
        {
            //do nothing
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            throw new InvalidOperationException("DataGridGoDetailsColumn can not be not IsReadOnly and can not generate editing element");
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            var hyperlinkButton = new HyperlinkButton
            {
                Margin = new Thickness(4),
                VerticalAlignment = VerticalAlignment.Center
            };
            hyperlinkButton.Click+=HyperlinkButtonOnClick;
            if (Binding != null )
            {
                hyperlinkButton.SetBinding(ContentControl.ContentProperty, Binding);
            }
            return hyperlinkButton;
        }

        private void HyperlinkButtonOnClick(object sender, RoutedEventArgs routedEventArgs)
        {
            var element = (FrameworkElement)sender;
            var selectableGrid = element.FindAncestor<jSLGrid>();
            selectableGrid.InvokeShowDetailsClicked(new RowSelectedEventArgs(element.DataContext));
        }
    }
}